package com.chengh.nami.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengh.nami.common.enums.common.StatusEnum;
import com.chengh.nami.common.enums.system.BusinessType;
import com.chengh.nami.common.enums.system.OperatorType;
import com.chengh.nami.common.exception.BizException;
import com.chengh.nami.common.request.PageQuery;
import com.chengh.nami.common.response.PageData;
import com.chengh.nami.common.utils.FileUtils;
import com.chengh.nami.common.utils.StringUtils;
import com.chengh.nami.system.domain.dto.OperLogDto;
import com.chengh.nami.system.mapper.SysOperLogMapper;
import com.chengh.nami.system.pojo.SysOperLog;
import com.chengh.nami.system.service.ISysOperLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {
    @Override
    public void insertOperlog(SysOperLog operLog) {

        this.baseMapper.insert(operLog);

    }

    @Override
    public PageData<SysOperLog> getList(OperLogDto operLogDto, PageQuery pageQuery) {
        Page<SysOperLog> page = pageQuery.build();
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<SysOperLog>().eq(StringUtils.isNotEmpty(operLogDto.getTitle()), SysOperLog::getTitle, operLogDto.getTitle()).eq(StringUtils.isNotEmpty(operLogDto.getBusinessType()), SysOperLog::getBusinessType, operLogDto.getBusinessType()).eq(StringUtils.isNotEmpty(operLogDto.getStatus()), SysOperLog::getStatus, operLogDto.getStatus()).like(StringUtils.isNotEmpty(operLogDto.getOperName()), SysOperLog::getOperName, operLogDto.getOperName()).ge(operLogDto.getBeginTime() != null, SysOperLog::getOperTime, operLogDto.getBeginTime()).le(operLogDto.getEndTime() != null, SysOperLog::getOperTime, operLogDto.getEndTime());
        Page<SysOperLog> sysOperLogPage = baseMapper.selectPage(page, wrapper);
        return PageData.build(sysOperLogPage);
    }

    @Override
    public void cleanOperLog() {
        baseMapper.cleanOperLog();
    }

    @Override
    public void exportLogininforExcle(HttpServletResponse response, OperLogDto operLogDto, PageQuery pageQuery) {
        PageData<SysOperLog> listPage = getList(operLogDto, pageQuery);
        List<SysOperLog> list = listPage.getRecords();
        if (CollectionUtils.isEmpty(list)) {
            new BizException("查询到的结果为空");
        }
        List<Map<String, Object>> operLogList = new ArrayList<>();
        for (SysOperLog sysOperLog : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("日志编号", sysOperLog.getId());
            map.put("操作人员", sysOperLog.getOperName());
            map.put("操作模块", sysOperLog.getTitle());
            map.put("业务类型", BusinessType.getInfoByCode(sysOperLog.getBusinessType()));
            map.put("请求方法", sysOperLog.getMethod());
            map.put("请求方式", sysOperLog.getRequestMethod());
            map.put("操作类别", OperatorType.getInfoByCode(sysOperLog.getOperatorType()));
            map.put("部门名称", sysOperLog.getDeptName());
            map.put("请求url", sysOperLog.getOperUrl());
            map.put("操作地址", sysOperLog.getOperIp());
            map.put("操作地点", sysOperLog.getOperLocation());
            map.put("请求参数", sysOperLog.getOperParam());
            map.put("返回参数", sysOperLog.getJsonResult());
            map.put("操作状态", StatusEnum.getInfoByCode(sysOperLog.getStatus()));
            map.put("错误消息", sysOperLog.getErrorMsg());
            map.put("操作时间", sysOperLog.getOperLocation());
            map.put("消耗时间毫秒", sysOperLog.getCostTime());
            operLogList.add(map);
        }
        try {
            FileUtils.downloadExcel(operLogList, response);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}
